﻿
CREATE proc [log].[UserIsLoggedIn]
@UserID int
as
begin
	declare @logTime datetime2(7) =  dateadd(MI, -3, sysdatetime()),
			@retTime datetime2(7),
			@ret bit = 0

	select	top 1 @retTime = case when u.[logType] = 'O' then null else u.[logDateTime] end
	from	log.hlpUserLog u
	where	u.UserID = @UserID
			and u.[logDateTime] > @logTime
			--and u.[logType] = 'I'
	order by u.[logDateTime] desc

	set @ret = case when @retTime is null then 0 else 1 end
	select	@UserID		'UserID',
			@retTime	'LogoutTime',
			@ret		'LoggedIn'

	return 0
end
